#!/usr/bin/perl
#
# $Id: Reunion2iCal,v 0.6 2004/09/22 13:43:00 d Exp $
#
#    Copyright 2004 Darrell Schulte <d@schulte.mn>
#
#    This file is part of Reunion2iCal.
#
#    Reunion2iCal is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    Reunion2iCal is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Reunion2iCal; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use Time::Local;

  use Getopt::Std;
  use vars qw($opt_a $opt_t $opt_i $opt_s $opt_e);
  my $optstr ="a:t:i:s:e:";
  getopts($optstr);
  
system "perl -i -pe 's/\015/\015\012/g' /Applications/Reunion2iCal/birthdaylist.tab";

open(IN, "$ARGV[0]") || die;

%months = ('Jan','01','Feb','02','Mar','03','Apr','04','May','05','Jun','06','Jul','07','Aug','08','Sep','09','Oct','10','Nov','11','Dec','12','jan','01','feb','02','mar','03','apr','04','may','05','jun','06','jul','07','aug','08','sep','09','oct','10','nov','11','dec','12','Maa','03','Mei','05','Okt','10','maa','03','mei','05','okt','10');

# Dutch
#%months = ('Maa','03','Mei','05','Okt','10','maa','03','mei','05','okt','10');

print "BEGIN:VCALENDAR\n";
print "VERSION:2.0\n";
print "X-WR-CALNAME:Birthday\n";
print "CALSCALE:GREGORIAN\n";
print "METHOD:PUBLISH\n";

while (<>) {
   ($year) = (localtime(time))[5];
   $year += 1900;

   $_ =~ s/\cM(\s)/\n/g;
   $_ =~ s/\cM/\n/g;
   $_ =~ s/Ò/"/g;
   $_ =~ s/Ó/"/g;
   $_ =~ s/Õ/'/g;
   ($f, $l, $b) = split(/\t/,$_);
   ($d, $m, $y) = split(/ /,$b);
   $y2k = substr($year,2,4,"");
   $nd = $d + 1;
   $dld = length($d);
   $dlnd = length($nd);
   if ($dld == "1") { $d = "0" . $d; }
   if ($dlnd == "1") { $nd = "0" . $nd; }
   
   # check birthday length
   $birthday = $year . $y2k . $months{$m} . $d;
   $bl = length($birthday);
   
if ($bl == "8") {

   print "BEGIN:VEVENT\n";
   print "DTSTART;VALUE=DATE:" . $birthday . "\n";
   print "DTEND;VALUE=DATE:" . $year . $y2k . $months{$m} . $nd . "\n";
   print "SUMMARY:" .$f . " " . $l. "\n";
   print "RRULE:FREQ=YEARLY;INTERVAL=1\n";
   
        if ($opt_a eq "Yes" && $opt_t eq "Message") {
            print "BEGIN:VALARM\n";
            print "TRIGGER:-P" . $opt_i . "D\n";
            print "DESCRIPTION:Event reminder\n"; 
            print "ACTION:DISPLAY\n";
            print "END:VALARM\n";
        }
   
        if ($opt_a eq "Yes" && $opt_t eq "Message with sound") {
            print "BEGIN:VALARM\n";
            print "TRIGGER:-P" . $opt_i . "D\n";
            print "ATTACH;VALUE=URI:" . $opt_s . "\n"; 
            print "ACTION:AUDIO\n";
            print "END:VALARM\n";
        }
   
        if ($opt_a eq "Yes" && $opt_t eq "Email") {
            print "BEGIN:VALARM\n";
            print "ACTION:EMAIL\n";
            print "SUMMARY:Alarm notification\n";
            print "TRIGGER:-P" . $opt_i . "D\n";
            print "ATTENDEE:" . $opt_e . "\n";
            print "DESCRIPTION:This is an event reminder\n";
            print "END:VALARM\n";
        }

   print "END:VEVENT\n";
   
} #birthday length check   
   
}

print "END:VCALENDAR\n";

close(IN);